﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using com.vs.DataAccess;
using com.vs.General;

namespace com.vs.DataAccess.DO
{
    public partial class DynamicCallingDetailDO : BaseDO
    {

        #region "Get Model"
        /// <summary>
        /// 将符合数据库主键的持久对象提取到模型对象中，如数据库符合条件的持久对象不存在，则返回空对象
        /// </summary>
        /// <remarks></remarks>
        public static DynamicCallingDetailDO GetModelByPK(Session sess, double dObjID)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            string stSQL = "SP_DYNAMICCALLINGDETAIL_SelectByPK";
            try
            {
                helper = new DataHelper(sess);
                dtModel = helper.GetTable(CommandType.StoredProcedure, stSQL, new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@OBJECT_ID", dObjID)
                });
                if (!DataManager.IsEmpty(dtModel))
                {
                    return GetModel(dtModel.Rows[0]);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        #endregion

        #region "DataBase Operation"
        /// <summary>
        /// 在数据库里创建该数据访问对象的持久化数据，同时自增长属性OBJECT_ID也会同时得到更新,并提供数据库事务支持
        /// </summary>
        /// <remarks></remarks>
        public static void Insert(Session sess, DynamicCallingDetailDO dcDtlDO)
        {
            DataHelper helper = null;
            IDataParameter[] paras = null;
            string stSQL = "SP_DYNAMICCALLINGDETAIL_Insert";
            try
            {
                helper = new DataHelper(sess);
                paras = ValueParas(dcDtlDO);
                helper.Insert(CommandType.StoredProcedure, stSQL, paras);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public static void Delete(Session sess, DynamicCallingDetailDO dcDtlDO)
        {
            DataHelper helper = null;
            string stSQL = "SP_DYNAMICCALLINGDETAIL_DeleteByPK";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, stSQL, UpdateWherePara(dcDtlDO));
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

        }

        /// <summary>
        /// 将该数据访问对象的变化同步到数据库中，如果该数据访问对象在数据库中的持久数据不存在，同步不会发生，同时也不会抛出例外
        /// </summary>
        /// <remarks></remarks>
        public static void Update(Session sess, DynamicCallingDetailDO dcDtlDO)
        {
            DataHelper helper = null;
            string stSQL = "SP_DYNAMICCALLINGDETAIL_Update";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, stSQL, ValueParas(dcDtlDO));
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

        }

        #endregion

        #region "Data Load"
        /// <summary>
        /// 装载对象DYNAMICCALLINGDETAIL的所有持久性对象到集合，如果数据量大，调用此方法会造成性能上的问题，请谨慎使用
        /// </summary>
        /// <remarks></remarks>
        public static List<DynamicCallingDetailDO> GetAll(Session sess)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            string stSQL = "SP_DYNAMICCALLINGDETAIL_SelectAll";
            try
            {
                helper = new DataHelper(sess);
                dtModel = helper.GetTable(CommandType.StoredProcedure, stSQL);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        /// <summary>
        /// 装载指定列的值等于指定值的的所有持久性DYNAMICCALLINGDETAIL对象到集合
        /// </summary>
        /// <remarks></remarks>
        public static List<DynamicCallingDetailDO> GetListByColumn(Session sess, string columnName, Object value)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            IDataParameter[] paras = null;
            string stSQL = null;
            try
            {
                helper = new DataHelper(sess);
                stSQL = SelectSequel + " Where [" + columnName + "] =@Value ";
                paras = new IDataParameter[]{
                    new SqlParameter( "@Value", value)
                };
                dtModel = helper.GetTable(CommandType.Text, stSQL, paras);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        /// <summary>
        /// 从DataView装载持久性DYNAMICCALLINGDETAIL对象到集合
        /// </summary>
        /// <remarks></remarks>
        private static List<DynamicCallingDetailDO> LoadListFromDataView(DataView dv)
        {
            List<DynamicCallingDetailDO> list = new List<DynamicCallingDetailDO>();
            for (int index = 0; index <= dv.Count - 1; index++)
            {
                list.Add(GetModel(dv[index].Row));
            }
            return list;
        }

        /// <summary>
        /// loading data value  to property from Datarow
        /// </summary>
        /// <remarks></remarks>
        private static DynamicCallingDetailDO GetModel(DataRow row)
        {
            DynamicCallingDetailDO dcDtlDO = new DynamicCallingDetailDO();
            if (!row["RECORD_TYPE"].Equals(DBNull.Value)) dcDtlDO.RECORD_TYPE = Convert.ToString(row["RECORD_TYPE"]);
            if (!row["IDENTIFIER"].Equals(DBNull.Value)) dcDtlDO.IDENTIFIER = Convert.ToString(row["IDENTIFIER"]);
            if (!row["DESCRIPTION"].Equals(DBNull.Value)) dcDtlDO.DESCRIPTION = Convert.ToString(row["DESCRIPTION"]);
            if (!row["PROCESS_PACKAGE"].Equals(DBNull.Value)) dcDtlDO.PROCESS_PACKAGE = Convert.ToString(row["PROCESS_PACKAGE"]);
            if (!row["PROCESS_CLASS"].Equals(DBNull.Value)) dcDtlDO.PROCESS_CLASS = Convert.ToString(row["PROCESS_CLASS"]);
            if (!row["PROCESS_METHOD"].Equals(DBNull.Value)) dcDtlDO.PROCESS_METHOD = Convert.ToString(row["PROCESS_METHOD"]);
            if (!row["SYSTEM_CREATED"].Equals(DBNull.Value)) dcDtlDO.SYSTEM_CREATED = Convert.ToString(row["SYSTEM_CREATED"]);
            if (!row["ACTIVE"].Equals(DBNull.Value)) dcDtlDO.ACTIVE = Convert.ToString(row["ACTIVE"]);
            if (!row["USER_DEF1"].Equals(DBNull.Value)) dcDtlDO.USER_DEF1 = Convert.ToString(row["USER_DEF1"]);
            if (!row["USER_DEF2"].Equals(DBNull.Value)) dcDtlDO.USER_DEF2 = Convert.ToString(row["USER_DEF2"]);
            if (!row["USER_DEF3"].Equals(DBNull.Value)) dcDtlDO.USER_DEF3 = Convert.ToString(row["USER_DEF3"]);
            if (!row["USER_DEF4"].Equals(DBNull.Value)) dcDtlDO.USER_DEF4 = Convert.ToString(row["USER_DEF4"]);
            if (!row["USER_DEF5"].Equals(DBNull.Value)) dcDtlDO.USER_DEF5 = Convert.ToString(row["USER_DEF5"]);
            if (!row["USER_DEF6"].Equals(DBNull.Value)) dcDtlDO.USER_DEF6 = Convert.ToString(row["USER_DEF6"]);
            if (!row["USER_DEF7"].Equals(DBNull.Value)) dcDtlDO.USER_DEF7 = Convert.ToDouble(row["USER_DEF7"]);
            if (!row["USER_DEF8"].Equals(DBNull.Value)) dcDtlDO.USER_DEF8 = Convert.ToDouble(row["USER_DEF8"]);
            if (!row["USER_STAMP"].Equals(DBNull.Value)) dcDtlDO.USER_STAMP = Convert.ToString(row["USER_STAMP"]);
            if (!row["PROCESS_STAMP"].Equals(DBNull.Value)) dcDtlDO.PROCESS_STAMP = Convert.ToString(row["PROCESS_STAMP"]);
            if (!row["DATE_TIME_STAMP"].Equals(DBNull.Value)) dcDtlDO.DATE_TIME_STAMP = Convert.ToDateTime(row["DATE_TIME_STAMP"]);
            if (!row["OBJECT_ID"].Equals(DBNull.Value)) dcDtlDO.OBJECT_ID = Convert.ToDouble(row["OBJECT_ID"]);
            if (!row["ASSEMBLY_NAME"].Equals(DBNull.Value)) dcDtlDO.ASSEMBLY_NAME = Convert.ToString(row["ASSEMBLY_NAME"]);
            if (!row["REMOTE_OBJECT_TYPE"].Equals(DBNull.Value)) dcDtlDO.REMOTE_OBJECT_TYPE = Convert.ToString(row["REMOTE_OBJECT_TYPE"]);
            if (!row["LOCAL_OBJECT_IDENTIFIER"].Equals(DBNull.Value)) dcDtlDO.LOCAL_OBJECT_IDENTIFIER = Convert.ToString(row["LOCAL_OBJECT_IDENTIFIER"]);
            if (!row["URI"].Equals(DBNull.Value)) dcDtlDO.URI = Convert.ToString(row["URI"]);
            if (!row["REQUEST_TRANSFORM"].Equals(DBNull.Value)) dcDtlDO.REQUEST_TRANSFORM = Convert.ToString(row["REQUEST_TRANSFORM"]);
            if (!row["RESPONSE_TRANSFORM"].Equals(DBNull.Value)) dcDtlDO.RESPONSE_TRANSFORM = Convert.ToString(row["RESPONSE_TRANSFORM"]);
            if (!row["OPERATION"].Equals(DBNull.Value)) dcDtlDO.OPERATION = Convert.ToString(row["OPERATION"]);
            if (!row["MESSAGE_NAME"].Equals(DBNull.Value)) dcDtlDO.MESSAGE_NAME = Convert.ToString(row["MESSAGE_NAME"]);
            if (!row["ENDPOINT_TYPE"].Equals(DBNull.Value)) dcDtlDO.ENDPOINT_TYPE = Convert.ToDouble(row["ENDPOINT_TYPE"]);
            dcDtlDO.IsPersist = true;

            return dcDtlDO;
        }

        #endregion

        #region "Other function"
        /// <summary>
        /// 该数据访问对象从数据库中提取数据的Sql语句 
        /// </summary>
        /// <remarks></remarks>
        private static string SelectSequel
        {
            get
            {
                return "Select	*  From [DYNAMIC_CALLING_DETAIL] ";
            }
        }

        /// <summary>
        /// 该数据访问对象的属性值装载到数据库更新参数数组
        /// </summary>
        /// <remarks></remarks>
        private static IDbDataParameter[] ValueParas(DynamicCallingDetailDO dcDtlDO)
        {
            SqlParameter[] paras = new SqlParameter[29];
            paras[0] = new SqlParameter("@RECORD_TYPE", dcDtlDO.RECORD_TYPE == String.Empty ? DBNull.Value : (object)dcDtlDO.RECORD_TYPE);
            paras[1] = new SqlParameter("@IDENTIFIER", dcDtlDO.IDENTIFIER == String.Empty ? DBNull.Value : (object)dcDtlDO.IDENTIFIER);
            paras[2] = new SqlParameter("@DESCRIPTION", dcDtlDO.DESCRIPTION == String.Empty ? DBNull.Value : (object)dcDtlDO.DESCRIPTION);
            paras[3] = new SqlParameter("@PROCESS_PACKAGE", dcDtlDO.PROCESS_PACKAGE == String.Empty ? DBNull.Value : (object)dcDtlDO.PROCESS_PACKAGE);
            paras[4] = new SqlParameter("@PROCESS_CLASS", dcDtlDO.PROCESS_CLASS == String.Empty ? DBNull.Value : (object)dcDtlDO.PROCESS_CLASS);
            paras[5] = new SqlParameter("@PROCESS_METHOD", dcDtlDO.PROCESS_METHOD == String.Empty ? DBNull.Value : (object)dcDtlDO.PROCESS_METHOD);
            paras[6] = new SqlParameter("@SYSTEM_CREATED", dcDtlDO.SYSTEM_CREATED == String.Empty ? DBNull.Value : (object)dcDtlDO.SYSTEM_CREATED);
            paras[7] = new SqlParameter("@ACTIVE", dcDtlDO.ACTIVE == String.Empty ? DBNull.Value : (object)dcDtlDO.ACTIVE);
            paras[8] = new SqlParameter("@USER_DEF1", dcDtlDO.USER_DEF1 == String.Empty ? DBNull.Value : (object)dcDtlDO.USER_DEF1);
            paras[9] = new SqlParameter("@USER_DEF2", dcDtlDO.USER_DEF2 == String.Empty ? DBNull.Value : (object)dcDtlDO.USER_DEF2);
            paras[10] = new SqlParameter("@USER_DEF3", dcDtlDO.USER_DEF3 == String.Empty ? DBNull.Value : (object)dcDtlDO.USER_DEF3);
            paras[11] = new SqlParameter("@USER_DEF4", dcDtlDO.USER_DEF4 == String.Empty ? DBNull.Value : (object)dcDtlDO.USER_DEF4);
            paras[12] = new SqlParameter("@USER_DEF5", dcDtlDO.USER_DEF5 == String.Empty ? DBNull.Value : (object)dcDtlDO.USER_DEF5);
            paras[13] = new SqlParameter("@USER_DEF6", dcDtlDO.USER_DEF6 == String.Empty ? DBNull.Value : (object)dcDtlDO.USER_DEF6);
            paras[14] = new SqlParameter("@USER_DEF7", dcDtlDO.USER_DEF7);
            paras[15] = new SqlParameter("@USER_DEF8", dcDtlDO.USER_DEF8);
            paras[16] = new SqlParameter("@USER_STAMP", dcDtlDO.USER_STAMP == String.Empty ? DBNull.Value : (object)dcDtlDO.USER_STAMP);
            paras[17] = new SqlParameter("@PROCESS_STAMP", dcDtlDO.PROCESS_STAMP == String.Empty ? DBNull.Value : (object)dcDtlDO.PROCESS_STAMP);
            paras[18] = new SqlParameter("@DATE_TIME_STAMP", dcDtlDO.DATE_TIME_STAMP == DateTime.MinValue ? DBNull.Value : (object)dcDtlDO.DATE_TIME_STAMP);
            paras[19] = new SqlParameter("@OBJECT_ID", dcDtlDO.OBJECT_ID);
            paras[20] = new SqlParameter("@ASSEMBLY_NAME", dcDtlDO.ASSEMBLY_NAME == String.Empty ? DBNull.Value : (object)dcDtlDO.ASSEMBLY_NAME);
            paras[21] = new SqlParameter("@REMOTE_OBJECT_TYPE", dcDtlDO.REMOTE_OBJECT_TYPE == String.Empty ? DBNull.Value : (object)dcDtlDO.REMOTE_OBJECT_TYPE);
            paras[22] = new SqlParameter("@LOCAL_OBJECT_IDENTIFIER", dcDtlDO.LOCAL_OBJECT_IDENTIFIER == String.Empty ? DBNull.Value : (object)dcDtlDO.LOCAL_OBJECT_IDENTIFIER);
            paras[23] = new SqlParameter("@URI", dcDtlDO.URI == String.Empty ? DBNull.Value : (object)dcDtlDO.URI);
            paras[24] = new SqlParameter("@REQUEST_TRANSFORM", dcDtlDO.REQUEST_TRANSFORM == String.Empty ? DBNull.Value : (object)dcDtlDO.REQUEST_TRANSFORM);
            paras[25] = new SqlParameter("@RESPONSE_TRANSFORM", dcDtlDO.RESPONSE_TRANSFORM == String.Empty ? DBNull.Value : (object)dcDtlDO.RESPONSE_TRANSFORM);
            paras[26] = new SqlParameter("@OPERATION", dcDtlDO.OPERATION == String.Empty ? DBNull.Value : (object)dcDtlDO.OPERATION);
            paras[27] = new SqlParameter("@MESSAGE_NAME", dcDtlDO.MESSAGE_NAME == String.Empty ? DBNull.Value : (object)dcDtlDO.MESSAGE_NAME);
            paras[28] = new SqlParameter("@ENDPOINT_TYPE", dcDtlDO.ENDPOINT_TYPE);

            paras[0].DbType = DbType.String;
            paras[1].DbType = DbType.String;
            paras[2].DbType = DbType.String;
            paras[3].DbType = DbType.String;
            paras[4].DbType = DbType.String;
            paras[5].DbType = DbType.String;
            paras[6].DbType = DbType.String;
            paras[7].DbType = DbType.String;
            paras[8].DbType = DbType.String;
            paras[9].DbType = DbType.String;
            paras[10].DbType = DbType.String;
            paras[11].DbType = DbType.String;
            paras[12].DbType = DbType.String;
            paras[13].DbType = DbType.String;
            paras[14].DbType = DbType.Decimal;
            paras[15].DbType = DbType.Decimal;
            paras[16].DbType = DbType.String;
            paras[17].DbType = DbType.String;
            paras[18].DbType = DbType.Date;
            paras[19].DbType = DbType.Decimal;
            paras[20].DbType = DbType.String;
            paras[21].DbType = DbType.String;
            paras[22].DbType = DbType.String;
            paras[23].DbType = DbType.String;
            paras[24].DbType = DbType.String;
            paras[25].DbType = DbType.String;
            paras[26].DbType = DbType.String;
            paras[27].DbType = DbType.String;
            paras[28].DbType = DbType.Decimal;

            return paras;
        }

        private static IDbDataParameter[] UpdateWherePara(DynamicCallingDetailDO dcDtlDO)
        {
            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@OBJECT_ID", dcDtlDO.OBJECT_ID);
            return paras;
        }

        #endregion

    }
}
